#include "stagemodepage.h"
#include "ui_stagemodepage.h"

StageModePage::StageModePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StageModePage)
{
    ui->setupUi(this);
    ui->stackedWidget->setCurrentIndex(0);
    numButton[0]=ui->pushButton_1;
    numButton[1]=ui->pushButton_2;
    numButton[2]=ui->pushButton_3;
    numButton[3]=ui->pushButton_4;

    connect(ui->jiaButton,&QPushButton::clicked,[=](){symbol=0;});
    connect(ui->jianButton,&QPushButton::clicked,[=](){symbol=1;});
    connect(ui->chengButton,&QPushButton::clicked,[=](){symbol=2;});
    connect(ui->chuButton,&QPushButton::clicked,[=](){symbol=3;});

    next();
}
void StageModePage::restart()
{
    ui->stackedWidget->setCurrentIndex(0);
    numButton[0]->show();
    numButton[1]->show();
    numButton[2]->show();
    numButton[3]->show();
    next();
}
void StageModePage::next()
{
    bu=0;
    ui->label_2->hide();
    if(ui->radioButton->isChecked())
    {
        int cnt=0;
        while (cnt <= 9)
        {
            pro.cre();
            cnt = pro.count();
        }
        ui->pushButton_1->setText(QString::number(pro.a));
        ui->pushButton_2->setText(QString::number(pro.b));
        ui->pushButton_3->setText(QString::number(pro.c));
        ui->pushButton_4->setText(QString::number(pro.d));
    }
    if(ui->radioButton_2->isChecked())
    {
        int cnt=0;
        while (cnt > 9 || cnt < 4)
        {
            pro.cre();
            cnt = pro.count();
        }
        ui->pushButton_1->setText(QString::number(pro.a));
        ui->pushButton_2->setText(QString::number(pro.b));
        ui->pushButton_3->setText(QString::number(pro.c));
        ui->pushButton_4->setText(QString::number(pro.d));
    }
    if(ui->radioButton_3->isChecked())
    {
        int cnt=0;
        while (cnt > 4 || cnt == 0)
        {
            pro.cre();
            cnt = pro.count();
        }
        ui->pushButton_1->setText(QString::number(pro.a));
        ui->pushButton_2->setText(QString::number(pro.b));
        ui->pushButton_3->setText(QString::number(pro.c));
        ui->pushButton_4->setText(QString::number(pro.d));
    }
    if(ui->radioButton_4->isChecked())
    {
        int cnt=0;
        while (cnt == 0)
        {
            pro.cre();
            cnt = pro.count();
        }
        ui->pushButton_1->setText(QString::number(pro.a));
        ui->pushButton_2->setText(QString::number(pro.b));
        ui->pushButton_3->setText(QString::number(pro.c));
        ui->pushButton_4->setText(QString::number(pro.d));
    }
    numButton[0]->show();
    numButton[1]->show();
    numButton[2]->show();
    numButton[3]->show();
}
StageModePage::~StageModePage()
{
    delete ui;
}

void StageModePage::on_pushButton_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void StageModePage::sleep(int msec)
{
    QTime dieTime = QTime::currentTime().addMSecs(msec);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

void StageModePage::operation(int b1,int b2)
{
    if(b1==b2)
    {
        numButton[b2]->setAutoExclusive(false);
        numButton[b2]->setChecked(false);
        numButton[b2]->setAutoExclusive(true);
        return;
    }
    Fraction f1,f2,f3;
    QString tmp=numButton[b2]->text();
    if(tmp.indexOf("/") >= 0)
    {
        Fraction f1;
        f1.up=tmp.section('/', 0, 0).trimmed().toInt();
        f1.down=tmp.section('/', 1, 1).trimmed().toInt();
    }else{
        f1.down=1;
        f1.up=tmp.toInt();
    }
        f2=f1;
        switch (symbol) {
        case 0:
            f3=Add(now,f2);
            if(f3.down==1)
            {
                numButton[b2]->setText(QString::number(f3.up,10));
            }else{
                numButton[b2]->setText(QString::number(f3.up,10)+"/"+QString::number(f3.down,10));
            }
            numButton[b1]->hide();
            state=false;
            bu++;
            break;
        case 1:
            f3=minu(now,f2);
            if(f3.down==1)
            {
                numButton[b2]->setText(QString::number(f3.up,10));
            }else{
                numButton[b2]->setText(QString::number(f3.up,10)+"/"+QString::number(f3.down,10));
            }
            numButton[b1]->hide();
            state=false;
            bu++;
            break;
        case 2:
            f3=multi(now,f2);
            if(f3.down==1)
            {
                numButton[b2]->setText(QString::number(f3.up,10));
            }else{
                numButton[b2]->setText(QString::number(f3.up,10)+"/"+QString::number(f3.down,10));
            }
            numButton[b1]->hide();
            state=false;
            bu++;
            break;
        case 3:
            f3=divide(now,f2);
            if(f3.down==1)
            {
                numButton[b2]->setText(QString::number(f3.up,10));
            }else{
                numButton[b2]->setText(QString::number(f3.up,10)+"/"+QString::number(f3.down,10));
            }
            numButton[b1]->hide();
            state=false;
            bu++;
            break;
    }
    if(bu==3)
    {
        if(numButton[b2]->text()=="24")
        {
           ui->label_2->show();
           ui->label_2->setText("你真聪明！");
           sleep(2000);
           bu=0;
           numButton[b2]->setAutoExclusive(false);
           numButton[b2]->setChecked(false);
           numButton[b2]->setAutoExclusive(true);
           next();
        }else{
        ui->label_2->show();
        ui->label_2->setText("真遗憾！没能筹齐24");
        sleep(2000);
        bu=0;
        numButton[b2]->setAutoExclusive(false);
        numButton[b2]->setChecked(false);
        numButton[b2]->setAutoExclusive(true);
        next();
        }
    }
    numButton[b2]->setAutoExclusive(false);
    numButton[b2]->setChecked(false);
    numButton[b2]->setAutoExclusive(true);
}
void StageModePage::on_pushButton_1_clicked()
{
    if(state==true)
    {
        operation(last,0);
    }else{
        state=true;
        Fraction f1;
        QString tmp=ui->pushButton_1->text();
        if(tmp.indexOf("/") >= 0)
        {
            f1.up=tmp.section('/', 0, 0).trimmed().toInt();
            f1.down=tmp.section('/', 1, 1).trimmed().toInt();
        }else{
            f1.down=1;
            f1.up=tmp.toInt();
        }
        now=f1;
        last=0;
    }
}


void StageModePage::on_pushButton_2_clicked()
{

    if(state==true)
    {
        operation(last,1);
    }else{
        state=true;
        Fraction f1;
        QString tmp=ui->pushButton_2->text();
        if(tmp.indexOf("/") >= 0)
        {
            f1.up=tmp.section('/', 0, 0).trimmed().toInt();
            f1.down=tmp.section('/', 1, 1).trimmed().toInt();
        }else{
            f1.down=1;
            f1.up=tmp.toInt();
        }
        now=f1;
        last=1;
    }
}
void StageModePage::on_pushButton_3_clicked()
{

    if(state==true)
    {
        operation(last,2);
    }else{
        state=true;
        Fraction f1;
        QString tmp=ui->pushButton_3->text();
        if(tmp.indexOf("/") >= 0)
        {
            f1.up=tmp.section('/', 0, 0).trimmed().toInt();
            f1.down=tmp.section('/', 1, 1).trimmed().toInt();
        }else{
            f1.down=1;
            f1.up=tmp.toInt();
        }
        now=f1;
        last=2;
    }
}



void StageModePage::on_pushButton_4_clicked()
{
    if(state==true)
    {
        operation(last,3);
    }else{
        state=true;
        Fraction f1;
        QString tmp=ui->pushButton_4->text();
        if(tmp.indexOf("/") >= 0)
        {
            f1.up=tmp.section('/', 0, 0).trimmed().toInt();
            f1.down=tmp.section('/', 1, 1).trimmed().toInt();
        }else{
            f1.down=1;
            f1.up=tmp.toInt();
        }
        now=f1;
        last=3;
    }
}

